package net.gyapp.markim.utils;

import android.content.Context;
import android.graphics.Bitmap;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

import java.security.MessageDigest;

/**
 * Auther:  winds
 * Data:    2017/10/25
 * Version: 1.0
 * Desc:
 */


public class GlideScaleBottomTransform extends BitmapTransformation {
    private float radio;

    public GlideScaleBottomTransform(Context context, float radio) {
        super(context);
        this.radio = radio;
    }


    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        return cropBitmap(pool, toTransform);
    }

    private Bitmap cropBitmap(BitmapPool pool, Bitmap source) {
        if (source == null) return null;

        Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        }
        float w = result.getWidth();
        float h = result.getHeight();
        float v = h / w;
        if (h > w && radio > 0 && v > radio) {
            float diff = v - radio;
            float start = diff * w;
            result = Bitmap.createBitmap(source, 0, (int) start, (int) w, (int) (h - start));
        }
        return result;
    }

    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {

    }
}
